package me.zhengjie.modules.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.domain.Companies;
import me.zhengjie.modules.repository.CompaniesMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseFinMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseInfMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseInvMapper;
import me.zhengjie.modules.repository.intellectual.*;
import me.zhengjie.modules.service.CompaniesService;
import me.zhengjie.modules.utils.DateUtils;
import org.checkerframework.checker.units.qual.m3;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("CompaniesService")
@RequiredArgsConstructor
public class CompaniesServiceImpl implements CompaniesService {

    private final CompaniesMapper companiesMapper;
    //企业基本信息
    private final EnterpriseInfMapper enterpriseInfMapper;
    //投融资
    private final EnterpriseFinMapper enterpriseFinMapper;
    private final EnterpriseInvMapper enterpriseInvMapper;

    //知识产权
    private final EnterIntTrademarkMapper trademarkMapper;
    private final EnterIntSoftwareMapper softwareMapper;
    private final EnterIntWorksMapper worksMapper;
    private final EnterIntWebSiteMapper webSiteMapper;
    private final EnterIntPatentMapper patentMapper;
    private final EnterpriseQuaMapper quaMapper;

    @Override
    public List<Companies> findAllByPage(Integer pageNo,Integer pageSize) {
        return companiesMapper.selectAllByPage(pageNo,pageSize);
    }

    @Override
    public int addCompaniesByList(List<Companies> list) {
        if(list.size()<=0||list==null){
            return 0;
        }
        int num=0;
        for (Companies companies : list) {
            num+=companiesMapper.insert(companies);
        }
        return num;
    }

    @Override
    public List<Companies> findAll() {
        return companiesMapper.selectAll();
    }

    @Override
    public int queryCount() {
        return companiesMapper.selectCount();
    }

    @Override
    public List<Map<String, Object>> queryAllCount() {
        List<Map<String, Object>> mapList=new ArrayList<>();
        Map<String, Object> map=new HashMap<>();
        int comTotal=companiesMapper.selectCount();
        List<Integer> list=enterpriseFinMapper.selectEnterpriseFinCount();
        int finTotal=list.stream().mapToInt(i->i).sum();
        int finEntTotal= list.size();
        List<Integer> list1=enterpriseInvMapper.selectEnterpriseInvCount();
        int invTotal=list1.stream().mapToInt(i->i).sum();
        int invEntTotal=list1.size();
        int patTotal=patentMapper.selectPatentCount();
        int softTotal=softwareMapper.selectSoftwareCount();
        int tramTotal=trademarkMapper.selectTrademarkCount();
        int quaTotal=quaMapper.selectEnterpriseQuaCount();
        List<Map<String,Object>> industryTopTen=enterpriseInfMapper.selectEnterpriseInfIndustryTopTen();
        map.put("comTotal",comTotal);
        map.put("finTotal",finTotal);
        map.put("invTotal",invTotal);
        map.put("patTotal",patTotal);
        map.put("softTotal",softTotal);
        map.put("tramTotal",tramTotal);
        map.put("quaTotal",quaTotal);
        map.put("industryTopTen",industryTopTen);
        map.put("finEntTotal",finEntTotal);
        map.put("invEntTotal",invEntTotal);
        List<String> date= DateUtils.getYear();
        List<String> year=new ArrayList<>();
        Map<String, Object> map1=new HashMap<>();
        List<Integer> patent=new ArrayList<>();
        List<Integer> software=new ArrayList<>();
        List<Integer> trademark=new ArrayList<>();
        List<Integer> qua=new ArrayList<>();
        List<Integer> inv=new ArrayList<>();
        List<Integer> fin=new ArrayList<>();
        Integer patMax=0;
        Integer finMax=0;
        for (String s : date) {
            Integer pat=patentMapper.selectPatentByYearToTotal(s);
            patMax=Math.max(patMax,pat);
            patent.add(pat);
            Integer soft=softwareMapper.selectSoftwareByYearToTotal(s);
            patMax=Math.max(patMax,soft);
            software.add(soft);
            Integer trade=trademarkMapper.selectTrademarkByYearToTotal(s);
            trademark.add(trade);
            patMax=Math.max(patMax,trade);
            Integer quaT=quaMapper.selectEnterpriseQuaByYearTotal(s);
            qua.add(quaT);
            patMax=Math.max(patMax,quaT);

            Integer invT=enterpriseInvMapper.selectEnterpriseInvByYearToTotal(s);
            finMax=Math.max(finMax,invT);
            inv.add(invT);
            Integer finT=enterpriseFinMapper.selectEnterpriseFinByYearCount(s);
            finMax=Math.max(finMax,finT);
            fin.add(finT);
        }
        map1.put("patentTotal",patent);
        map1.put("software",software);
        map1.put("trademarkTotal",trademark);
        map1.put("quaTotal",qua);
        map1.put("invTotal",inv);
        map1.put("finTotal",fin);
        map.put("year",year);
        map.put("patMax",patMax);
        map.put("finMax",finMax);
        mapList.add(map);
        mapList.add(map1);
        return mapList;
    }

    @Override
    public List<Map<String, Object>> queryAllCountByCondition(Integer honor,Integer honor2) {
        List<Map<String, Object>> mapList=new ArrayList<>();
        Map<String, Object> map=new HashMap<>();
        int comTotal=companiesMapper.selectCount();
        List<Integer> list=enterpriseFinMapper.selectEnterpriseFinCountByCondition(honor2);
        int finTotal=list.stream().mapToInt(i->i).sum();
        int finEntTotal= list.size();
        List<Integer> list1=enterpriseInvMapper.selectEnterpriseInvCountByCondition(honor2);
        int invTotal=list1.stream().mapToInt(i->i).sum();
        int invEntTotal=list1.size();
        int patTotal=patentMapper.selectPatentCountByCondition(honor);
        int softTotal=softwareMapper.selectSoftwareCountByCondition(honor);
        int tramTotal=trademarkMapper.selectTrademarkCountByCondition(honor);
        int quaTotal=quaMapper.selectEnterpriseQuaCountByCondition(honor);
        List<Map<String,Object>> industryTopTen=enterpriseInfMapper.selectEnterpriseInfIndustryTopTen();
        map.put("comTotal",comTotal);
        map.put("finTotal",finTotal);
        map.put("invTotal",invTotal);
        map.put("patTotal",patTotal);
        map.put("softTotal",softTotal);
        map.put("tramTotal",tramTotal);
        map.put("quaTotal",quaTotal);
        map.put("industryTopTen",industryTopTen);
        map.put("finEntTotal",finEntTotal);
        map.put("invEntTotal",invEntTotal);
        List<String> date= DateUtils.getYear();
        List<String> year=new ArrayList<>();
        Map<String, Object> map1=new HashMap<>();
        List<Integer> patent=new ArrayList<>();
        List<Integer> software=new ArrayList<>();
        List<Integer> trademark=new ArrayList<>();
        List<Integer> qua=new ArrayList<>();
        List<Integer> inv=new ArrayList<>();
        List<Integer> fin=new ArrayList<>();
        Integer patMax=0;
        Integer finMax=0;
        Map<String,Object>  map2=null;
        for (String s : date) {
            map2=new HashMap<>();
            map2.put("date",s);
            map2.put("honor",honor);
            map2.put("honor2",honor2);
            Integer pat=patentMapper.selectPatentByYearToTotalOnMap(map2);
            patMax=Math.max(patMax,pat);
            patent.add(pat);
            Integer soft=softwareMapper.selectSoftwareByYearToTotalOnMap(map2);
            patMax=Math.max(patMax,soft);
            software.add(soft);
            Integer trade=trademarkMapper.selectTrademarkByYearToTotalOnMap(map2);
            trademark.add(trade);
            patMax=Math.max(patMax,trade);
            Integer quaT=quaMapper.selectEnterpriseQuaByYearTotalOnMap(map2);
            qua.add(quaT);
            patMax=Math.max(patMax,quaT);
            Integer invT=enterpriseInvMapper.selectEnterpriseInvByYearAndConditionToTotal(map2);
            finMax=Math.max(finMax,invT);
            inv.add(invT);
            Integer finT=enterpriseFinMapper.selectEnterpriseFinByYearAndConditionCount(map2);
            finMax=Math.max(finMax,finT);
            fin.add(finT);
        }
        map1.put("patentTotal",patent);
        map1.put("software",software);
        map1.put("trademarkTotal",trademark);
        map1.put("quaTotal",qua);
        map1.put("invTotal",inv);
        map1.put("finTotal",fin);
        map.put("year",year);
        map.put("patMax",patMax);
        map.put("finMax",finMax);
        mapList.add(map);
        mapList.add(map1);
        return mapList;
    }
}
